﻿using log4net;
using McsUtility;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MinerManager.Forms
{
	public partial class WalletInsert : Form
	{
		ILog logger = LogManager.GetLogger(typeof(Program));
		private Common.AgentComponent Agent = null;

		private int CurrencyId = 0;
		
		public event EventHandler<EventArgs> OnSuccess;
		
		public WalletInsert(Common.AgentComponent agent, int currencyId)
		{
			InitializeComponent();
			this.Agent = agent;
			this.CurrencyId = currencyId;
		}

		#region 窗体事件

		private void WalletInsert_Load(object sender, EventArgs e)
		{
			this.Agent.OnAddWalletReceive += new EventHandler<ByteEventArgs>(Agent_OnAddWalletReceive);

			//币种
			List<ComboboxListItem> items = new List<ComboboxListItem>();
			for (int i = 0; i < Common.Data.UserCurrency.Count; i++)
			{
				items.Add(new ComboboxListItem { Text = Common.Data.UserCurrency[i].Remark, Value = Common.Data.UserCurrency[i].ID.ToString() });
			}
			ItemCurrency.DataSource = items;
			ItemCurrency.DisplayMember = "Text";
			ItemCurrency.ValueMember = "Value";
			
			if (items.Count(p => p.Value == this.CurrencyId.ToString()) > 0)
				ItemCurrency.SelectedValue = this.CurrencyId.ToString();
		}

		private void WalletInsert_FormClosing(object sender, FormClosingEventArgs e)
		{
			this.Agent.OnAddWalletReceive -= new EventHandler<ByteEventArgs>(Agent_OnAddWalletReceive);
		}

		#endregion

		#region void Agent_OnAddWalletReceive(object sender, ByteEventArgs e)

		private void Agent_OnAddWalletReceive(object sender, ByteEventArgs e)
		{
			McsUtility.Models.ResultModel result = ProtocolHelper.ReadPackageBody<McsUtility.Models.ResultModel>(e.Bytes);
			if (result.success)
			{
				OnSuccess?.Invoke(this, new EventArgs());
				Common.Utility.FormClose(this);
			}
			else
			{
				MessageBox.Show("钱包保存失败，" + result.message + "。", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
		}

		#endregion

		#region void BtnSave_Click(object sender, EventArgs e)

		private void BtnSave_Click(object sender, EventArgs e)
		{
			//非法信息检测
			if (ItemCurrency.SelectedValue == null || string.IsNullOrWhiteSpace(ItemCurrency.SelectedValue.ToString()))
			{
				if (MessageBox.Show("请选择币种。", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error) == DialogResult.OK)
				{
					ItemCurrency.Focus();
					ItemCurrency.SelectAll();
				}
				return;
			}

			if (string.IsNullOrWhiteSpace(ItemAddress.Text))
			{
				if (MessageBox.Show("请填写钱包地址。", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error) == DialogResult.OK)
				{
					ItemAddress.Focus();
					ItemAddress.SelectAll();
				}
				return;
			}

			//发送数据
			if (this.Agent.IsStarted)
			{
				McsUtility.Models.WalletsModel info = new McsUtility.Models.WalletsModel
				{
					ID = ItemCurrency.SelectedValue.ToString().ConvertInt(),
					Address = ItemAddress.Text.Trim(),
					Remark = ItemRemark.Text
				};
				byte[] bytes = ProtocolHelper.WritePackage<McsUtility.Models.WalletsModel>(ProtoType.MinerManagerAddWallets, info);
				this.Agent.AgentSend(bytes);
			}
			else
			{
				MessageBox.Show("与服务器的链接断开，请重新登录系统。", "错误", MessageBoxButtons.OK, MessageBoxIcon.Warning);
			}
		}

		#endregion

		#region void BtnClose_Click(object sender, EventArgs e)

		private void BtnClose_Click(object sender, EventArgs e)
		{
			this.Close();
		}

		#endregion



		#region void TextBox_KeyPress(object sender, KeyPressEventArgs e)

		private void TextBox_KeyPress(object sender, KeyPressEventArgs e)
		{
			if (e.KeyChar == 13)
			{
				e.Handled = true;
				BtnSave_Click(BtnSave, new EventArgs());
			}

			if (e.KeyChar == 27)
			{
				BtnClose_Click(BtnClose, new EventArgs());
			}
		}

		#endregion
	}
}
